// pages/fileprint/fileprint.js
const app =getApp()
Page({

  
  data: {   url:'',near_id:'',goods_id:'',last:''

  },

  onLoad: function (options) { 
     
      
      this.setData({
        near_id:options.near_id,
        goods_id:options.goods_id
      })
      console.log(this.data.near_id);
      
  },
  //微信文件打印
  wechatfile(){
      this.throttle(()=>{
          
      wx.chooseMessageFile({
        count: 1,
        type:'file',
        success:(res1)=>{
          wx.showLoading({
            title: '正在上传...',
            mask:true
          })
          console.log(res1);
          let filepath=res1.tempFiles[0].path
           this.upload(filepath,"https://zujieba.com/Api/upload/uploadFile.html").then(res=>{
             console.log(res);
             
            let data=JSON.parse(res.data)
              if(data.success==1){
                console.log(data);
               let url="https://zujieba.com/uploads/"+data.name
               console.log(url);
               
               let goods_id
               if(this.data.goods_id){
                    goods_id=this.data.goods_id
               }else{
                  goods_id=''
               }
              
              
                wx.request({
                  url: 'https://zujieba.com/Api/test2/printing.html',
                  data:{goods_id:goods_id,file:data.name},
                  success:(res2)=>{
                    wx.hideLoading()
                    
                    console.log(res2);
                    /*let imgurl=res2.data.data.data.task_result.data.img_list
                    let page=imgurl.length
                    app.globalData.arr=imgurl*/
                   if(res2.data.success==2){
                     wx.showToast({
                       title: res2.data.data.msg,
                       icon:'none'
                     })
                     return
                   }
                   if(res2.data.data.data.pages==0){
                    wx.showToast({
                      title: '获取文档页数失败',
                      
                      icon:'none'
                    })
                    return
                  }
                    // wx.navigateTo({
                    //   url: '/pages/fileprint/print/print?url='+url+"&name="+data.name+"&page="+res2.data.data.data.pages+"&filename="+res1.tempFiles[0].name+"&time="+data.time+'&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+'&typearr=1'+'&flag2=1'+"&param=1",
                    // })
                    wx.navigateTo({
                      url: '/pages/fileprint/print/newprint?url='+url+"&name="+data.name+"&page="+res2.data.data.data.pages+"&filename="+res1.tempFiles[0].name+"&time="+data.time+'&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+'&typearr=1'+'&flag2=1'+"&param=1",
                    })
                  },
                  fail:(res)=>{
                    console.log(res);
                    wx.hideLoading()
                    wx.showToast({
                      title:'请求超时,请稍后再试',
                      icon:'none'
                      
                   })
                  }
                  

                })
           /*     wx.request({
                  url: 'https://zujieba.com/Api/test2/page.html',
                  data:{goods_id:goods_id,file:data.name,type:1,status:1},
                  success:(res2)=>{
                    console.log(res2)
                    if(res2.data.success==1){
                      wx.request({
                        url: 'https://zujieba.com/Api/test2/page.html',
                        data:{goods_id:goods_id,file:data.name,type:4,status:7},
                        success:(res3)=>{
                          console.log(res3)
                        }
                      })
                    }
                   
                  }
                })*/
              
               
               
              }else{
                 wx.showToast({
                   title: data.tips,
                   icon:'none'
                 })
              }
           })
         
          }
          
          
          
          
        
        
      })
      },1500)
  },
  getceshi(file){
    wx.request({
      url: 'http://cloud.wisiyilink.com/api/print/file_pages',
      method:'POST',
      header:{ApiKey:'ueQr4lbU5lurtgz6D0xilLMcHErzDBOb','Content-Type':'multipart/form-data'},
      data:{deviceId:'ps01cz01526843',deviceKey:'HWgtLbhd7BH4vWTl',devicePort:1,printerModel:'HP Color LaserJet Pro MFP M477 PCL-6',
      dmPaperSize:9,dmOrientation:1,jobFile:file},
      
      success:(res2)=>{
        console.log(res2);
        
        var title
        if(res2.data.data.task_state=='READY'){
           title='排队中'
        }else if(res2.data.data.task_state=='PARSING'){
           title='解析中'
        }else if(res2.data.data.task_state=='SENDING'){
          title='发送中'
        }else if(res2.data.data.task_state=='SUCCESS'){
          title='成功'
        }else if(res2.data.data.task_state=='FAILURE'){
          title='失败'
        }
        wx.showLoading({
          title: title,
        })
        console.log(res2);
        if(res2.data.data.task_state=='SUCCESS'){
        console.log(111);
        
         
        }else if(res2.data.data.task_state=='FAILUER'){
          wx.hideLoading()
        }else{
          this.getceshi(task_id)
        }
        
        
      },
      fail:(res)=>{
        console.log(res);
       
        
      }
    })
  },
 
  //本地文件打印  跳转
     navlocalprint(){
       console.log(this.data.near_id);
       
          wx.navigateTo({
            url: '/pages/localprint/localprint?near_id='+this.data.near_id+"&goods_id="+this.data.goods_id,
          })
         
     },
     //拍照打印
     navphotoprint(){
       wx.chooseImage({
         count:1,
         success:(res)=>{
           console.log(res);
           let path=res.tempFiles[0].path
            wx.uploadFile({
              filePath: path,
              name: 'file',
              formData: { user: 'test' },
              url: "https://zujieba.com/Api/upload/upload",
              success:(res1)=>{
              let data=JSON.parse(res1.data)
              console.log(data);
              let url="https://zujieba.com/uploads/"+data.name
                console.log(url);
                this.setData({
                  url
                })
                // wx.navigateTo({
                //   url: '/pages/fileprint/print/print?url='+url+"&near_id="+this.data.near_id+"&goods_id="+this.data.goods_id+"&time="+data.time+"&name="+data.name+"&photo=1",
                // })
                wx.navigateTo({
                  url: '/pages/fileprint/print/newprint?url='+url+"&near_id="+this.data.near_id+"&goods_id="+this.data.goods_id+"&time="+data.time+"&name="+data.name+"&photo=1",
                })
                
              
              
                
              }
            })
           
         }
       })
     },
     //微信文件上传
     upload(filepath,url){
        return new Promise((resolve,rej)=>{
             wx.uploadFile({
               filePath: filepath,
               name: 'file',
               url: url,
               success:(res)=>{
                 console.log(res);
                 
                 return resolve(res)
               },
               fail:(res)=>{
                 return rej(res)
               }
             })
        })
     },
     //跳转到文档打印
     navdocument(){
       wx.navigateTo({
         url: '/pages/document/document?near_id='+this.data.near_id+"&goods_id="+this.data.goods_id,
       })
     },
     //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
    showfilemodal(){
       this.setData({
         filemodal:true
       })
    },
    modalclose(){
      this.setData({
        filemodal:false
      })
    },
    // 复制文件打印
    copy(){
     
     wx.getClipboardData().then(res=>{
       wx.showLoading({
         title: '正在上传',
         icon:'none'
       })
       wx.request({
         url: 'https://zujieba.com/Api/upload/down_pic.html',
         data:{url:res.data},
         method:'POST',
         success:(res)=>{
           if(res.data.success==1){
            let arr=res.data.data.url.split('uploads/')
           console.log(res);
           if(res.data.data.type=='docx'||res.data.data.type=='xls'||res.data.data.type=='pdf'||res.data.data.type=='doc'||res.data.data.type=='ppt'||res.data.data.type=='pptx'||res.data.data.type=='xlsx'){
            
            wx.request({
              url: 'https://zujieba.com/Api/test2/printing.html',
              data:{goods_id:this.data.goods_id,file:arr[1]},
              success:(res2)=>{
                wx.hideLoading()
                
                console.log(res2);
                /*let imgurl=res2.data.data.data.task_result.data.img_list
                let page=imgurl.length
                app.globalData.arr=imgurl*/
               if(res2.data.success==2){
                 wx.showToast({
                   title: res2.data.data.msg,
                   icon:'none'
                 })
                 return
               }
               if(res2.data.data.data.pages==0){
                wx.showToast({
                  title: '获取文档页数失败',
                  
                  icon:'none'
                })
                return
              }
              
             
                
         
         wx.navigateTo({
           url: '/pages/fileprint/print/newprint?url='+res.data.data.url+"&name="+arr[1]+"&page="+res2.data.data.data.pages+"&filename="+arr[1]+'&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+'&typearr=1'+'&flag2=1'+"&param=1",
         })
              
              
              
              },
              fail:(res)=>{
                console.log(res);
                wx.hideLoading()
                wx.showToast({
                  title:'请求超时,请稍后再试',
                  icon:'none'
                  
               })
              }
              

            })
          
           }else{
            wx.navigateTo({
              url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+arr[1]+
              '&time=2021'+'&page=1'+"&url="+res.data.data.url+'&radio5=4'+"&photo=1"+'&param1=1'
            })
           }
          }else{
            wx.showToast({
              title: res.data.tips,
              icon:'none'
            })
          }
         }
       })
       
     })
    },
    copytext(){
      wx.showLoading({
        title: '正在上传',
        icon:'none'
      })
      wx.getClipboardData().then(res=>{
        if(res.data.trim()==''){
          wx.showToast({
            title: '请复制内容',
            icon:'none'
          })
          return
        }
        wx.request({
          url: 'https://zujieba.com/Api/upload/word.html',
          data:{title:res.data},
          method:'POST',
          success:(res)=>{
            let arr=res.data.data.split('uploads/')
            console.log(res);
          
            wx.request({
              url: 'https://zujieba.com/Api/test2/printing.html',
              data:{goods_id:this.data.goods_id,file:arr[1]},
              success:(res2)=>{
                wx.hideLoading()
                
                console.log(res2);
               
               if(res2.data.success==2){
                 wx.showToast({
                   title: res2.data.data.msg,
                   icon:'none'
                 })
                 return
               }
               if(res2.data.data.data.pages==0){
                wx.showToast({
                  title: '获取文档页数失败',
                  
                  icon:'none'
                })
                return
              }
                // wx.navigateTo({
                //   url: '/pages/fileprint/print/print?url='+url+"&name="+data.name+"&page="+res2.data.data.data.pages+"&filename="+res1.tempFiles[0].name+"&time="+data.time+'&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+'&typearr=1'+'&flag2=1'+"&param=1",
                // })
                wx.navigateTo({
                  url: '/pages/fileprint/print/newprint?url='+res.data.data+"&name="+arr[1]+"&page="+res2.data.data.data.pages+"&filename="+arr[1]+
                  '&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+'&typearr=1'+'&flag2=1'+"&param=1",
                })
              
              },
              fail:(res)=>{
                console.log(res);
                wx.hideLoading()
                wx.showToast({
                  title:'请求超时,请稍后再试',
                  icon:'none'
                  
               })
              }
              

            })





           
          }})})
    }
  
})